package android.java.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

public class DataBaseHelper {
	private static Connection conn;
	private static PreparedStatement ps;
	private static ResultSet rs;
	public static boolean insert(String table,HashMap<String,Object> maps){
		boolean isSuccess=false;
		if(maps==null||table==null){
			return isSuccess;
		}
		StringBuilder builder=new StringBuilder();
		builder.append("insert into "+table+"(");
		Object[] keys= maps.keySet().toArray();
		Object[] values=new Object[maps.size()];
		for(int i=0;i<maps.size();i++){
			values[i]=maps.get(keys[i]);
		}
		for(int i=0;i<keys.length;i++){
			if(keys.length-1==i){
				builder.append(keys[i]);
			}else{
				builder.append(keys[i]+",");
			}
		}
		builder.append(") values(");
		for(int i=0;i<values.length;i++){
			if(values.length-1==i){
				if(values[i] instanceof Boolean){
					builder.append(values[i]);
				}else if(values[i] instanceof Integer){
					builder.append(values[i]);
				}else{
					builder.append("'"+values[i]+"'");
				}
			}else{
				if(values[i] instanceof Boolean){
					builder.append(values[i]+",");
				}else if(values[i] instanceof Integer){
					builder.append(values[i]+",");
				}else{
					builder.append("'"+values[i]+"'"+",");
				}
			}
		}
		builder.append(")");
		System.out.println("sql:"+builder.toString());
		conn=new DatabaseUtil().getConn();
		try {
			ps=conn.prepareStatement(builder.toString());
			ps.executeUpdate();
			isSuccess=true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new DatabaseUtil().close(conn, ps, rs);
		return isSuccess;
	}
	public ResultSet querry(Connection conn,String table,String[] columns,String[] where,Object[] whereValues,String order){
		StringBuilder builder=new StringBuilder();
		builder.append("select ");
		if(columns==null){
			builder.append("*");
		}else{
			for(int i=0;i<columns.length;i++){
				if(i==columns.length-1){
					builder.append(columns[i]);
				}else{
					builder.append(columns[i]+",");
				}
			}
		}
		builder.append(" from "+table);
		if(where!=null){
			builder.append(" where ");
			for(int i=0;i<where.length;i++){
				if(i==where.length-1){
					builder.append(where[i]+"="+"? ");
				}else{
					builder.append(where[i]+"="+"?"+" and ");
				}
			}
		}
		if(order!=null){
			builder.append("order by "+order);
		}
		try {
			ps=conn.prepareStatement(builder.toString());
			for(int i=0;i<whereValues.length;i++){
				if(whereValues[i] instanceof Boolean){
					ps.setBoolean(i+1,(Boolean) whereValues[i]);
				}else if(whereValues[i] instanceof Integer){
					ps.setInt(i+1, (Integer) whereValues[i]);
				}else{
					ps.setString(i+1,(String) whereValues[i]);
				}
			}
			rs=ps.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//new DatabaseUtil().close(conn, ps, rs);
		return rs;
	}
}
